/*
 * Copyright 2020-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yb.xczx.cloud.auth.deserializer;

import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.yb.xczx.cloud.auth.bean.XczxSysUser;
import com.yb.xczx.cloud.auth.bean.XczxUserPassAuthenticationToken;
import com.yb.xczx.cloud.auth.bean.vo.XczxSysUserVo;
import com.yb.xczx.cloud.auth.util.JsonNodeUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest.Builder;
import org.springframework.util.CollectionUtils;

/**
 * A {@code JsonDeserializer} for {@link OAuth2AuthorizationRequest}.
 *
 * @author Joe Grandja
 * @since 0.1.2
 * @see 
 * @see 
 */
public final class XczxUserPassAuthenticationTokenDeserializer extends JsonDeserializer<XczxUserPassAuthenticationToken> {

	@Override
	public XczxUserPassAuthenticationToken deserialize(JsonParser parser, DeserializationContext context)
			throws IOException {
		ObjectMapper mapper = (ObjectMapper) parser.getCodec();
		JsonNode root = mapper.readTree(parser);
		return deserialize(parser, mapper, root);
	}

	private XczxUserPassAuthenticationToken deserialize(JsonParser parser, ObjectMapper mapper, JsonNode root)
			throws JsonParseException {

		XczxSysUser xczxSysUser = JsonNodeUtils.findValue(root, "xczxSysUser", new TypeReference<XczxSysUser>() {
		}, mapper);

		List<SimpleGrantedAuthority> authorities = JsonNodeUtils.findValue(root, "authorities", new TypeReference<List<SimpleGrantedAuthority>>() {
		},mapper);

		xczxSysUser.setAuthorities(new HashSet<>(authorities));

		XczxSysUser newSysUser = new XczxSysUser(xczxSysUser.getSysUser(), xczxSysUser.getAuthorities());

		return XczxUserPassAuthenticationToken.authenticated(newSysUser,newSysUser.getAuthorities());
	}

	private Builder getBuilder(JsonParser parser,
			AuthorizationGrantType authorizationGrantType) throws JsonParseException {
		if (AuthorizationGrantType.AUTHORIZATION_CODE.equals(authorizationGrantType)) {
			return OAuth2AuthorizationRequest.authorizationCode();
		}
		throw new JsonParseException(parser, "Invalid authorizationGrantType");
	}

	private static AuthorizationGrantType convertAuthorizationGrantType(JsonNode jsonNode) {
		String value = JsonNodeUtils.findStringValue(jsonNode, "value");
		if (AuthorizationGrantType.AUTHORIZATION_CODE.getValue().equalsIgnoreCase(value)) {
			return AuthorizationGrantType.AUTHORIZATION_CODE;
		}
		return null;
	}

}
